package com.tedu.springboot2302.controller;

import com.tedu.springboot2302.entity.Article;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    private static File articleDir;
    static {
        articleDir = new File("./articles");
        if(!articleDir.exists()) {
            articleDir.mkdirs();
        }
    }

    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request,HttpServletResponse response) {
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String text = request.getParameter("text");

        Article article = new Article(title,author,text);

        File file = new File(articleDir,title+".obj");

        if(file.exists() || author==null || author.isEmpty() || title==null || title.isEmpty() || text==null || text.isEmpty()) {
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))
        ) {
            oos.writeObject(article);
            response.sendRedirect("/article_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    @RequestMapping("/readArticle")
//    public void readAArticle(HttpServletRequest request,HttpServletResponse response) {
//        response.setContentType("text/html;charset=utf-8");
//        Article art = null;
//        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(articleDir));) {
//            PrintWriter pw = response.getWriter();
//            pw.println("<!DOCTYPE html>\n" +
//                    "<html lang=\"en\">\n" +
//                    "<head>\n" +
//                    "    <meta charset=\"UTF-8\">\n" +
//                    "    <title>文章内容</title>\n" +
//                    "</head>\n" +
//                    "<body>\n" +
//                    "    <center>\n" +
//                    "        <table>");
//            pw.println("<tr><h1>文章标题</h1></tr>")
//            art = (Article)ois.readObject();
//        } catch (IOException | ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//
//        //"+ art.getTitle() +"
//        //"+ art.getAuthor() +"
//        //"+ art.getText() +"
//
//    }

    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request,HttpServletResponse response) {
        List<Article> articleList = new ArrayList<>();  //---------------泛型！！

        response.setContentType("text/html;charset=utf-8");

        File[] file = articleDir.listFiles(f -> f.getName().endsWith(".obj"));
        for(File f : file) {
            try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
            ) {
                Article article = (Article)ois.readObject();
                articleList.add(article);  //------------------------装对象
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        try {
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>\n" +
                    "<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <title>文章列表</title>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "  <center>\n" +
                    "    <h1>文章列表信息</h1>\n" +
                    "<table border='1'>"+
                    "    <tr>\n" +
                    "      <td>题目</td>\n" +
                    "      <td>作者</td>\n" +
                    "    </tr>");
            for(Article art : articleList) {
                pw.println("<tr>");
                pw.println("<td><a href=\"/readArticle\">"+ art.getTitle() +"</a></td>");
                pw.println("<td>"+ art.getAuthor() +"</td>");
                pw.println("</tr>");
            }
            pw.println("</table>\n"+"</center>\n" +
                    "</body>\n" +
                    "</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
